/* 
Set directory 
*/

    global root ENTER DIRECTORY
	
	global data 		$root\Dta\Young Lives\

	
********************************************************************************	
*Merging Descriptive Variables with Test Score and Test TIme Data
use "$data\OC R1234.dta", clear	


append using "$data\YC R1234.dta"

merge 1:1 childid round using "$data\all_ts.dta"

drop if idate == .
drop if _m == 2 & round != 4


drop _merge

merge 1:1 childid round using "$data\all_time.dta"

drop if _m == 2

drop _merge

********************************************************************************
* Cleaning
********************************************************************************
*Dropping children who were not in school in round 3

foreach num of numlist 1/10{
bys childid: replace enrschr3 = enrschr3[_n-`num'] if enrschr3 == .
bys childid: replace enrschr3 = enrschr3[_n+`num'] if enrschr3 == .
}

keep if enrschr3 == 1

*Dropping children who were not in school in round 4
foreach num of numlist 1/10{
bys childid: replace enrschr4 = enrschr4[_n-`num'] if enrschr4 == .
bys childid: replace enrschr4 = enrschr4[_n+`num'] if enrschr4 == .
}

gen sch_r4 = 0
replace sch_r4 = 1 if enrschr4 == 1 & enrschr4 != .

keep if sch_r4 == 1

*keep if n_raw_math !=. | n_raw_ppvt != .


*	Caste
tab chldeth, gen (caste)
	
*	Sex
tab sex,gen(male)
	drop male2
	ren male1 male
	lab define male 0 "female" 1 "male"
	lab val male male
	lab var male "Male"


*	Eldest child
generate eldest=0 if round == 2
replace eldest=1 if bornbef==0
drop bornbef
lab var eldest "Eldest child"

*	Urban

	tab typesite if round == 3, gen(urban)
	drop urban2 urban3 				// 17 cases with typesite NK assumed to be rural
	ren urban1 urban

	lab define urban 0 "rural" 1 "urban"
	lab val urban urban
	lab var urban "Urban location"

replace urban=1 if (clustid==1|clustid==3|clustid==10|clustid==14|clustid==20 ) & round == 1
replace urban=0 if (clustid==2|clustid==4|clustid==5|clustid==6|clustid==7|clustid==8|clustid==9|clustid==11|clustid==12|clustid==13|clustid==15|clustid==16|clustid==17|clustid==18) & round == 1


*Mother's Education
gen mum_ed = mumed
replace mum_ed = 15 if mum_ed == 28
replace mum_ed = 16 if mum_ed == 29
replace mum_ed = 0 if mum_ed == 77
replace mum_ed = 0 if mum_ed == 88

replace mum_ed = 1 if mum_ed>1 & mum_ed != .

*Father's Education
gen dad_ed = daded
replace dad_ed = 15 if dad_ed == 28
replace dad_ed = 0 if dad_ed == 77
replace dad_ed = 0 if dad_ed == 88


replace dad_ed = 1 if dad_ed>1 & dad_ed != .

foreach var of varlist caste2 caste3 eldest urban male dad_ed mum_ed {
foreach num of numlist 1/10{
bys childid: replace `var' = `var'[_n-`num'] if `var' == .
bys childid: replace `var' = `var'[_n+`num'] if `var' == .
}
}

*Age
gen age = .
replace age = 1 if round == 1 & cohort == 0
replace age = 5 if round == 2 & cohort == 0
replace age = 8 if round == 3 & cohort == 0
replace age = 10 if round == 4 & cohort == 0
replace age = 12 if round == 5 & cohort == 0

replace age = 8 if round == 1 & cohort == 1
replace age = 12 if round == 2 & cohort == 1
replace age = 15 if round == 3 & cohort == 1
replace age = 19 if round == 5 & cohort == 1


sort childid round
order childid round cohort

* Wealth Index
gen wealth = wi if round == 1
foreach num of numlist 1/5{
replace wealth = wealth[_n+`num'] if wealth == .
}

*Time Use
gen c_sleep = sleep
replace c_sleep = sleepr3 if c_sleep == .
replace c_sleep = sleepr4 if c_sleep == .

gen c_chcare = chcare
replace c_chcare = crothr3 if c_chcare == .
replace c_chcare = crothr4 if c_chcare == .
replace c_chcare = chcarer3 if c_chcare == .
replace c_chcare = chcarer4 if c_chcare == .

gen c_hhchore = hhchore
replace c_hhchore = dmtskr3 if c_hhchore == .
replace c_hhchore = dmtskr4 if c_hhchore == .
replace c_hhchore = hhchrer3 if c_hhchore == .
replace c_hhchore = hhchrer4 if c_hhchore == .

gen c_npaywork = npaywork
replace c_npaywork = tsfarmr3 if c_npaywork == .
replace c_npaywork = tsfarmr4 if c_npaywork == .
replace c_npaywork = npywrkr3 if c_npaywork == .
replace c_npaywork = npywrkr4 if c_npaywork == .

gen c_paywork = paywork
replace c_paywork = actpayr3 if c_paywork == .
replace c_paywork = actpayr4 if c_paywork == .
replace c_paywork = paywrkr3 if c_paywork == .
replace c_paywork = paywrkr4 if c_paywork == .

gen c_school = school
replace c_school = atschr3 if c_school == .
replace c_school = atschr4 if c_school == .
replace c_school = schoolr3 if c_school == .
replace c_school = schoolr4 if c_school == .

gen c_study = study
replace c_study = studygr3 if c_study == .
replace c_study = studygr4 if c_study == .
replace c_study = studyr3 if c_study == .
replace c_study = studyr4 if c_study == .

gen c_play = play
replace c_play = lsurer3 if c_play == .
replace c_play = lsurer4 if c_play == .
replace c_play = playr3 if c_play == .
replace c_play = playr4 if c_play == .


save "$data\yls_all_data.dta", replace
